<template>
  <div>
    <div class="kk_top">
      <el-divider></el-divider>
      <!-- <el-button @click="setData('teth')">生成teth</el-button>
      <el-button @click="setData('puj')">生成puj</el-button>-->
      <!-- <el-button @click="setData('puj')">生成puj</el-button> -->
      <el-button @click="getData">获取数据</el-button>
      <el-divider></el-divider>
    </div>

    <trading-vue
      :data="chart"
      :width="this.width"
      :height="this.height"
      :color-back="colors.colorBack"
      :color-grid="colors.colorGrid"
      :color-text="colors.colorText"
    ></trading-vue>
  </div>
</template>

<script>
import TradingVue from "trading-vue-js";

export default {
  name: "",
  components: { TradingVue },

  data() {
    return {
      num: 0,
      data: [],
      max: 0,
      min: 0,
      chart: {},
      width: window.innerWidth - 100,
      height: 650,
      colors: {
        colorBack: "#fff",
        colorGrid: "rgba(255,255,255,.1)",
        colorText: "#000"
      }
    };
  },
  mounted() {
    this.chart = {
      ohlcv: []
    };
    // setInterval(() => {
    //   this.getData();
    // }, 1500);
  },
  methods: {
    // 更新数据
    changeData() {
      if (this.data) {
        let kchart = {
          ohlcv: this.data
        };
        this.chart = kchart;
      }
    },
    setData(symbol) {
      let data = {
        symbol: symbol
      };
      this.$http.post("/auto_price_minute_day/index.html", data).then(res => {
        console.log(res);
      });
    },
    getData() {
      this.$http.post("/show").then(res => {
        console.log(res.data.data);
        let data = res.data.data;
        for (let i = 0; i < data.length; i++) {
          data[i][0] = data[i][0] * 1000;
        }
        this.data = data;
        this.changeData();
      });
    },
    ema(data, id) {
      let chartEMA = {
        name: "EMA," + id,
        type: "EMA",
        data: [],
        settings: {}
      };
      let ema = [];
      for (let i = 0; i < data.length; i++) {
        let a = data[i][4];
        //   let b = data[i - 1][4];
        let t = data[i][0];

        if (i == 0) {
          let box = [];
          box.push(t);
          box.push((a - a) / id + a);
          ema.push(box);
        } else {
          //   console.log(ema);

          let b = ema[i - 1][1];
          let box = [];
          box.push(t);
          box.push((a - b) / id + b);
          ema.push(box);
        }
      }
      chartEMA.data = ema;
      return chartEMA;
    }
  }
};
</script>

<style lang="less" scoped>
</style>
